package com.linked.basemanage.generallog.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.linked.basemanage.generallog.bean.param.LogListParam;
import com.linked.basemanage.generallog.bean.po.LogInfoPO;
import com.linked.basemanage.generallog.service.IGeneralLogService;
import com.linked.commonentity.basemanage.generallog.LinkedGeneralLog;
import com.linked.universal.common.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author :dbq
 * @date : 2023/1/16 9:31
 * @description : desc
 */
@Slf4j
@RestController
@RequestMapping("generallog")
public class GeneralLogController {

    private final IGeneralLogService generalLogService;

    private final ObjectMapper mapper;

    @Autowired
    public GeneralLogController(IGeneralLogService generalLogService, ObjectMapper mapper) {
        this.generalLogService = generalLogService;
        this.mapper = mapper;
    }

    @PostMapping("insertGeneralLog")
    void insertGeneralLog(@RequestBody LogInfoPO param) throws Exception {
        if (log.isInfoEnabled()) {
            log.info("插入日志接口,入参:{}", mapper.writeValueAsString(param));
        }
        generalLogService.insertGeneralLog(param);
    }

    @PostMapping("insertListkedLog")
    void insertListkedLog(@RequestBody LinkedGeneralLog param) {

        try {
            generalLogService.insertGeneralLog(new LogInfoPO(param));
        } catch (Exception e) {
            log.error("异常参数：", param.toString());
            log.error("添加日志异常！", e);
        }
    }


    @PostMapping("queryLogList")
    Result queryLogList(@RequestBody LogListParam param) throws Exception {
        List<LogInfoPO> res = null;
        try {
            res = generalLogService.queryLogList(param);
        } catch (Exception e) {
            log.error("查询通用日志列表异常！", e);
            return Result.error("查询通用日志列表异常！");
        }
        return CollectionUtils.isEmpty(res) ? Result.ok(false, "未查询到数据") : Result.success(res);
    }

}
